package com.hb.trade.server.project.section.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.core.pojo.project.section.entity.BidDownRecord;
import com.hb.trade.server.project.section.manager.BidDownRecordManager;
import com.hb.trade.server.project.section.service.raw.BidDownRecordService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 供应商招标文件下载记录表 前端控制器
 * </p>
 *
 * @author zhongdl
 * @since 2021-12-31
 */
@RestController
public class BidDownRecordController {

    protected final BidDownRecordService bidDownRecordService;

    public BidDownRecordController(BidDownRecordService bidDownRecordService) {
        this.bidDownRecordService = bidDownRecordService;
    }

    /**
     *
     * @Description: 投标人下载文件记录
     *
     * @Author: zhongdl
     * @Date:2022/1/4 11:15
     * @param bidDownRecord:
     * @return: {@link Message< PageResult< BidSectionDTO>> }
     **/
    @PostMapping("/bidDownRecord/saveBidDownRecord")
    public Message<BidDownRecord> saveBidDownRecord(@RequestBody BidDownRecord bidDownRecord){
         String msg = bidDownRecord.paramsValid("sectionId");
         if (StrUtil.isNotEmpty(msg)){
             return MsgUtil.fail(msg);
         }
         //填充招标文件id和下载时间
         BidDownRecordManager bidDownRecordManager = bidDownRecord.to(BidDownRecordManager.class);
         bidDownRecordManager.fillField();

         //判断是否下载了招标文件
        BidDownRecord downRecord = bidDownRecordService.getBidDownRecord(bidDownRecord.getSectionId(), bidDownRecord.getBidderId(), bidDownRecord.getOrgId());
        if (downRecord != null && downRecord.getId() != null){
            bidDownRecordManager.setId(downRecord.getId());
        }

        bidDownRecordService.saveOrUpdate(bidDownRecordManager);
         return MsgUtil.success(bidDownRecordManager.to(BidDownRecord.class));
    }
    /**
    * Description: 获取企业下载招标文件列表
    * @author: fengbin
    * @date: 2022/3/1 13:30
    * @param orgId
     * @param sectionId
     @return: org.fzzn.core.model.msg.Message<com.hb.core.pojo.project.section.entity.BidDownRecord>
    */
    @PostMapping("/bidDownRecord/findBidDownRecordInfoByOrgId")
    public Message<BidDownRecord> findBidDownRecordInfoByOrgId(@RequestParam("orgId") Long orgId , @RequestParam("sectionId") Long sectionId ){
        BidDownRecord bidDownRecordInfoByOrgId = bidDownRecordService.findBidDownRecordInfoByOrgId(orgId, sectionId);
        return MsgUtil.success(bidDownRecordInfoByOrgId);
    }

    /**
     * 根据多个标段查询下载信息
     *
     * @author yuezheng
     * @param sectionIdList
     * @return List<BidDownRecord>
     * @date 2022/4/16 4:31 PM
     */
    @PostMapping("/bidDownRecord/findBidDownRecordBySectionIdList")
    public Message< List<BidDownRecord>> findBidDownRecordBySectionIdList(@RequestBody List<Long> sectionIdList) {
        QueryWrapper<BidDownRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(BidDownRecord::getId,BidDownRecord::getBidderId, BidDownRecord::getSectionId).in(BidDownRecord::getSectionId,sectionIdList);
        List<BidDownRecord> list = this.bidDownRecordService.list(queryWrapper);
        return MsgUtil.success(list);
    }

}
